using System.Collections.Generic;

namespace HappyNumber
{
    public class Solution
    {
        public bool IsHappy(int n)
        {
            List<int> sums = new List<int>();
            var defaultNumber = n;
            while (true)
            {
                var sum = 0;
                while (n != 0)
                {
                    sum += n % 10 * (n % 10);
                    n /= 10;
                }

                if (sum == 1)
                {
                    return true;
                }

                if (sums.Contains(sum))
                {
                    return false;
                }
                else
                {
                    sums.Add(sum);
                }

                n = sum;
            }
        }
    }
}